//
//  FlowCheckVC.swift
//  Employee
//
//  Created by swordjoy on 2017/6/29.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

import UIKit
import IQKeyboardManager

class FlowCheckVC: BaseVC {
  
  var flowId: String!
  var isAllow: Bool = false
  var refreshBefore: VoidClosure?
  
  fileprivate var scrollView: UIScrollView!
  fileprivate var textView: PlaceholderTextView!
  fileprivate var submitButton: ObserveButton!
  fileprivate var sendModel: CheckSend = CheckSend()
  
  override func viewDidLoad() {
    super.viewDidLoad()
    create()
  }
  
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    GlobalConfig.baiduStatisticsPageViewStart("审批流程")
  }
  
  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    textView.textView.becomeFirstResponder()
  }
  
  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    
    GlobalConfig.baiduStatisticsPageViewEnd("流程审批")
  }
  
}

// MARK: - action

extension FlowCheckVC {
  
  @objc fileprivate func submitAction() {
    SJApp.endEdit()
    SJApp.showHUD()
    sendModel.explain = textView.textView.text!
    sendModel.id = flowId
    if isAllow { sendModel.type = "1" }
    else { sendModel.type = "0" }
    let _ = FlowRequest.shared.checkApply(model: sendModel, success: { 
      SJHint.show("操作成功")
      self.navigationController?.popViewController(animated: true)
      self.refreshBefore?()
    }, failure: nil)
  }
  
}


// MARK: - fileprivate

extension FlowCheckVC {
  
  fileprivate func create() {
    view.backgroundColor = GlobalConfig.BGWhiteColor
    
    addBackTitleItem(title: "取消")
    
    let rightItem = UIBarButtonItem.init(title: "确定", style: .plain, target: self, action: #selector(submitAction))
    navigationItem.rightBarButtonItem = rightItem
    
    scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.noNavBarHeight))
    scrollView.showsVerticalScrollIndicator = false
    scrollView.isScrollEnabled = false
    scrollView.contentSize = CGSize(width: UIScreen.width, height: UIScreen.noNavBarHeight)
    view.addSubview(scrollView)
    
    textView = PlaceholderTextView(frame: CGRect(x: 10, y: 10, width: UIScreen.width - 20, height: 200))
    textView.maxInput = 50
    scrollView.addSubview(textView)
    textView.layer.cornerRadius = 2
    textView.clipsToBounds = true
    
    if isAllow {
      title = "通过意见"
      textView.placeholderLabel.text = "通过意见说明(选填,50字以内)"
    }
    else {
      title = "不通过意见"
      textView.placeholderLabel.text = "不通过意见说明(选填,50字以内)"
    }
    
  }
  
}
